/**
 * 
 */
package com.xbang.attendance.web.action.delivery.deliverylist;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.order.DeliveryOrder;
import com.xbang.attendance.service.DeliveryOrderService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class Index extends SearchActionSupport {

	private static final long serialVersionUID = 6137752167041062321L;

	private static final Log logger = LogFactory.getLog(Index.class);

	@Autowired
	private DeliveryOrderService deliveryOrderService;

	private List<DeliveryOrder> deliveryOrders = new ArrayList<DeliveryOrder>();
	private Integer id;

	private String ids;

	public Integer getCurrPageId1() {
		return currPageId1;
	}
	public void setCurrPageId1(Integer currPageId1) {
		this.currPageId1 = currPageId1;
	}

	private Integer currPageId1;
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {

		return super.execute();
	}
	
	@Action("/delivery/deliverylist/index/${currPageId}")
	public String listByPage() throws Exception {
	//	System.out.println(currPageId);
		return list();
	}

	@Action("/delivery/deliverylist/list")
	public String list() throws Exception {

		String hqlSentence = "FROM DeliveryOrder deliveryorder WHERE 1 = 1";

		if (searchComponent.getOrderParameters().isEmpty()) {
			searchComponent.addSearchElement("orderBy.deliveryorder.deliveryDate", "DESC");
		}
		if (page != null) {
			if (currPageId.intValue() != -1 && currPageId.intValue() > 0) {
				page.setCurrentPage(currPageId);
			}
		}

		Result result = deliveryOrderService.searchByPage(hqlSentence, searchComponent, page);

		deliveryOrders = result.getItems();

		page = result.getPage();

		return "index";
	}

	@Action("/delevery/deliverylist/sendmail/${id}")
	public String sendMail() throws Exception {
		send(new String[]{id.toString()});
		return "r:/delivery/deliverylist/index/"+currPageId1;
	}

	@Action("/delevery/deliverylist/sendmails")
	public String sendMails() throws Exception {
		String[] idss = ids.split(",");
		send(idss);
		return "r:/delivery/deliverylist/index/"+currPageId1;
	}
	
	@Action("/delivery/deliverylist/changestatus")
	public String changeStatus(){
		String[] idss = ids.split(",");
		try {
			deliveryOrderService.updateStatus(idss);
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return "r:/delivery/deliverylist/index/"+currPageId1;
	}
	
	private void send(String [] idss) throws Exception{
		String webPath =ServletActionContext.getRequest().getRealPath("//download//order//delivery");
		String templatePath =ServletActionContext.getRequest().getRealPath("//template//送货清单.xls");
		deliveryOrderService.sendMail(webPath, templatePath, idss);
	}
	
	@Override
	public void resetCustomerSearchCriteria() {

	}

	/**
	 * @return the deliveryOrders
	 */
	public List<DeliveryOrder> getDeliveryOrders() {
		return deliveryOrders;
	}

	/**
	 * @param deliveryOrders
	 *            the deliveryOrders to set
	 */
	public void setDeliveryOrders(List<DeliveryOrder> deliveryOrders) {
		this.deliveryOrders = deliveryOrders;
	}

	/**
	 * @param deliveryOrderService
	 *            the deliveryOrderService to set
	 */
	public void setDeliveryOrderService(DeliveryOrderService deliveryOrderService) {
		this.deliveryOrderService = deliveryOrderService;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
